// pages/storeCollect/storeCollect.js
var util = require('../../utils/util.js');
Page({
  data: {
    selected: false,
    hasfinished: true, //管理、完成
    store: [], //已售出收藏店铺列表
    storeNum: 0, //初始化店铺数量
    selectAllStatus: false, // 全选状态，默认全选
    store_id: '', //店铺收藏
    storeSelectdId: [], //选中删除店铺的id
    JSONstoreSelectdId: '',
    cur_page: 1,
    page_num: 10,
  },
  onLoad: function() {
    this.getList();
  },
  getList: function() {
    var that = this;
    util.postUrl('member_favorites/store_list', {
      cur_page: that.data.cur_page,
      page_num: that.data.page_num,
    }, function(res) {
      if (res.data.code == 200) {
        var store = that.data.store;
        var cur_page = that.data.cur_page;
        var temp_list = res.data.datas.favorites_list;
        if (temp_list.length > 0) {
          cur_page++;
        }
        store = store.concat(temp_list);
        that.setData({
          cur_page: cur_page,
          store: temp_list,
        });
      }
    })
  },
  onShow: function() {},
  // 删除店铺：
  deleteStore: function(e) {
    var that = this;
    var storeIndex = e.currentTarget.dataset.index;
    var store = that.data.store;
    let storeSelectdId = store[storeIndex].store_id;

    console.log('bbbbbbbb', JSON.stringify(Array(storeSelectdId)));
    util.postUrl('member_favorites/store_del', {
      store_id_list: JSON.stringify(Array(storeSelectdId))
    }, function(res) {
      if (res.data.code == 200) {
        console.log('vvvvvvvvvvvvvv', res)
      }
    })
    store.splice(storeIndex, 1);
    that.setData({
      store: store
    });
    if (!store.length) {
      that.setData({
        hasStoreList: false,
      })
    }
  },
  // 管理按钮
  Management: function() {
    var value = !this.data.hasfinished;
    this.setData({
      hasfinished: value
    })
  },
  // 当前商品选中事件
  selectList: function(e) {
    const index = e.currentTarget.dataset.index;
    let store = this.data.store;
    const selected = store[index].selected;
    let storeSelectdId = store[index].store_id;
    storeSelectdId = this.data.storeSelectdId.push(storeSelectdId)
    console.log('aaaaaaaaaa', JSON.stringify(this.data.storeSelectdId));
    store[index].selected = !selected;
    this.setData({
      selectAllStatus: false,
      store: store,
      JSONstoreSelectdId: JSON.stringify(this.data.storeSelectdId)
    });
  },
  // 全选：
  selectAll(e) {
    var that = this;
    let selectAllStatus = that.data.selectAllStatus;
    selectAllStatus = !selectAllStatus;
    let storeSelectdId = that.data.storeSelectdId;
    let store = that.data.store;

    if (selectAllStatus == true) {
      for (let item of store) {
        storeSelectdId.push(item.store_id);
      }
    } else if (selectAllStatus == false) {
      storeSelectdId.splice(0, storeSelectdId.length)
    }

    for (let i = 0; i < store.length; i++) {
      store[i].selected = selectAllStatus
    }

    this.setData({
      selectAllStatus: selectAllStatus,
      store: store,
      JSONstoreSelectdId: JSON.stringify(this.data.storeSelectdId)
    });
  },
  // 取消收藏
  cancelCollect: function(e) {
    var temp = [];
    var that = this;
    var store = this.data.store;
    util.postUrl('member_favorites/store_del', {
      store_id_list: this.data.JSONstoreSelectdId
    }, function(res) {
      if (res.data.code == 200) {
        console.log('11111111111111', res)
      }
    })
    for (var i = 0; i < store.length; i++) {
      if (!store[i].selected) {
        temp.push(store[i]);
      }
    }
    if (!temp.length) {
      that.setData({
        hasStoreList: false
      })
    }
    that.setData({
      store: temp
    })
  },
  goStoreDetails(e) {
    var store_id = e.currentTarget.dataset.item.store_id;
    wx.navigateTo({
      url: '../shop_detail/shop_detail?store_id=' + store_id,
    })
  }
})